<script setup>

import {doLogout, getLoginUser} from "~/api/userApi.js";
import {userInfoStore} from "~/stores/userInfoStore.js";
import {successCode} from "~/api/base/http.js";
import {useRouter} from 'vue-router'
import MyTag from "~/components/constant/myTag.vue";
import {success} from "~/utils/myMessage.js";
import {adminUrl} from "~/utils/const.js";


let props = defineProps({
  userLogin: {
    type: Object,
    required: false,
    default: {
      imageUrl: "",
      name: "未登录",
      powerName: "未登录",
      money: "0.00",
      amount: "0.00",
    }
  }
})
const router = useRouter()

function logout() {
  doLogout().then(res => {
    if (res.code === successCode) {
      success(res.message);
      userInfoStore().logout();
      router.push("/login?redirect=login");
    }
  })
}

function goToAdmin() {
  window.open(adminUrl, '_blank');
}

function goToUserInfo() {
  router.push("/user-info/info");
}

function goToProfile() {
  router.push("/profile/" + userInfoStore().loginUser.id);
}
</script>

<template>
  <div class="user-base-msg-box">
    <div class="user-avatar-box">
      <el-avatar :size="60" :src="userLogin.imageUrl"/>
    </div>
    <div class="user-info-box">
      <div class="user-other-box">
        <div class="none-box"/>
        <div class="user-name-box">
          {{ userLogin.name }}
        </div>
        <div class="user-power-box">
          <my-tag :data="userLogin.powerName" :mapping="userInfoStore().getPowerTableOption()"/>
        </div>
        <div class="user-role-box">
          <!--          {{ userLogin.roleName }}-->
        </div>
        <div class="border-bottom-box"/>
        <div class="user-msg-box">
          <div class="user-msg-item">
            <div class="msg-title">
              余 额
            </div>
            <div class="msg-value">
              {{ userLogin.money === null ? "0.00" : userLogin.money }}
            </div>
          </div>
          <div class="user-msg-item">
            <div class="msg-title">
              累计消费
            </div>
            <div class="msg-value">
              {{ userLogin.amount === null ? "0.00" : userLogin.amount }}
            </div>
          </div>
        </div>
        <div class="border-bottom-box"/>
        <div class="user-btn-box">
          <div class="user-btn-item" @click="goToProfile()">
            <!--            <span :class="`iconfont ${icon}`"></span>-->
            <div class="item-value iconfont icon-wode"></div>
            <div class="item-value">个人主页</div>
          </div>
          <div class="user-btn-item" @click="goToUserInfo()">
            <!--            <span :class="`iconfont ${icon}`"></span>-->
            <div class="item-value iconfont icon-gerenzhongxin"></div>
            <div class="item-value">个人中心</div>
          </div>
          <div class="user-btn-item" v-if="userInfoStore().admin" @click="goToAdmin()">
            <div class="item-value iconfont icon-guanli-jiemiangexinghua"></div>
            <div class="item-value">管理员界面</div>
          </div>
          <div class="user-btn-item" @click="logout()">
            <div class="item-value iconfont icon-tuichu"></div>
            <div class="item-value">退出账号</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.user-base-msg-box {
  display: flex;
  position: relative;
  align-items: center;
  flex-direction: column;
  margin: .3rem 0;
  border-radius: 50%;

  .user-avatar-box {
    transition: all 0.8s ease; /* 设置过渡效果 */
    z-index: 99 !important;
  }

  .user-info-box {
    position: absolute;
    top: 90%;
    display: flex;
    opacity: 0;
    visibility: hidden; /* 隐藏元素 */
    align-items: center;
    flex-direction: column;
    cursor: unset;
    transition: all 0.5s ease; /* 设置过渡效果 */
    z-index: 9 !important;

    .user-other-box {
      background-color: #ffffff;
      margin-top: .2rem;
      width: 18.75rem;
      border-radius: .2rem;

      .user-triangle-box {
        height: 0.625rem;
        width: 0.625rem;
      }

      .none-box {
        height: 1.8rem;
      }

      .user-name-box,
      .user-role-box,
      .user-power-box {
        text-align: center;
        padding: .3rem;
        font-size: 1rem;
        font-weight: bolder;
      }

      .border-bottom-box {
        border-bottom: .1rem solid #e8e8ed;
      }

      .user-msg-box {
        display: flex;
        align-items: center;
        justify-content: space-around;
        padding: .8rem;

        .user-msg-item {
          display: flex;
          flex-direction: column;
          align-items: center;

          .msg-title {
            color: #999aaa;
          }

          .msg-value {
            font-size: 1.1rem;
            font-weight: bolder;
          }
        }
      }

      .user-btn-box {
        display: flex;
        align-items: center;
        flex-direction: column;

        .user-btn-item {
          display: flex;
          align-items: center;
          justify-content: center;
          width: 100%;
          cursor: pointer;
          margin: .3rem 0;
          padding: .5rem .3rem;
          height: 2rem;

          .iconfont {
            font-size: 1.2rem;
          }

          .item-value {
            padding: .1rem .2rem;
          }
        }

        .user-btn-item:hover {
          background-color: #f0f0f5;
        }
      }
    }
  }
}

.user-base-msg-box:hover .user-info-box {
  opacity: 1;
  visibility: visible; /* 可见 */
}

.user-base-msg-box:hover .user-avatar-box {
  transform: translateY(1.7rem) scale(1.2);
}
</style>